package com.google.code.avajhelpdesk;

import org.apache.wicket.Application;
import org.apache.wicket.authentication.AuthenticatedWebApplication;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.spring.ISpringContextLocator;
import org.apache.wicket.spring.injection.annot.AnnotSpringInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.google.code.avajhelpdesk.pages.HomePage;
import com.google.code.avajhelpdesk.pages.auth.SignInPage;


/**
 * Application object for the web application.
 * 
 */
public class WicketApplication extends AuthenticatedWebApplication implements ApplicationContextAware {   
	
	@SuppressWarnings("serial")
	private static final class ISpringContextLocatorImpl implements
			ISpringContextLocator {
		public ApplicationContext getSpringContext() {
			return ((WicketApplication)Application.get()).internalGetContext();
		}
	}

	private static final Logger logger = LoggerFactory.getLogger(WicketApplication.class);
	
	private ApplicationContext applicationContext;
		
	public WicketApplication() {
	}

	@Override
	protected void init() {
		super.init();
		
		getApplicationSettings().setAccessDeniedPage(SignInPage.class);
		
		mountBookmarkablePage("/login", SignInPage.class);
		
		InjectorHolder.setInjector(new AnnotSpringInjector(new ISpringContextLocatorImpl()));
	}

	/**
	 * Returns home page class for the application
	 * 
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	public Class<HomePage> getHomePage() {
		return HomePage.class;
	}

	@Override
	protected Class<? extends WebPage> getSignInPageClass() {
		return SignInPage.class;
	}

	@Override
	protected Class<? extends AuthenticatedWebSession> getWebSessionClass() {
		return MyWebSession.class;
	}

	/*
	 * =============== getters and setters ===================
	 */
	
	protected ApplicationContext internalGetContext() {
		return applicationContext;
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
}